<?php 

namespace pmvc\aop;

use pmvc\ioc\ObjectPostProcessor;

class LoggingInterceptor
	implements ObjectPostProcessor,
	MethodInterceptor {

	private $names 	= array();
	private $ids 	= array();

	function processBeforeInitialization($name, $id, $object) {
		if ($object!=$this
			&& ((pmvc_hasVal($id) && in_array($id, $this->ids))
				|| (pmvc_hasVal($name) && in_array($name, $this->names)))) {
			return new Proxy($object, $this);
		}
		return $object;
	}

	function processAfterInitialization($name, $id, $object) {
		
	}

	public function setNames($names) {
		$this->names = $names;
	}

	public function setIds($ids) {
		$this->ids = $ids;
	}

	function methodInvoked($object, $methodName, &$args, $userData=NULL) {
		$time = time();
		$ret = call_user_func_array(array($object, $methodName), $args);
		$elapsed = time()-$time;
		\pmvc\log\Log::debug("Method {0}::{1} invoked in {2} second(s)", get_class($object), $methodName, $elapsed);
		return $ret;
	}

}


?>